<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="医院名称">
        <el-input v-model="hospitalSet.hosname" />
      </el-form-item>
      <el-form-item label="医院编号">
        <el-input v-model="hospitalSet.hoscode" />
      </el-form-item>
      <el-form-item label="API地址">
        <el-input v-model="hospitalSet.apiUrl" />
      </el-form-item>
      <el-form-item label="联系人">
        <el-input v-model="hospitalSet.contactsName" />
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="hospitalSet.contactsPhone" />
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import hospitalSetApi from "@/api/hospitalSet";
export default {
  data() {
    return {
      //医院设置对象
      hospitalSet: {},
      //保存按钮是否禁用
      saveBtnDisabled: false,
    };
  },
  created() {
    //页面渲染之前获取路由参数中的Id回显数据
    if (this.$route.params && this.$route.params.id) {
      const hospitalSetId = this.$route.params.id;
      this.getHospitalSetById(hospitalSetId);
    }
  },
  methods: {
    saveOrUpdate() {
      //防多触
      this.saveBtnDisabled = true;
      if(this.hospitalSet.id){
         this.update()
      }else{
        this.save();
      }
    },
    save() {
      hospitalSetApi
        .saveHospitalSet(this.hospitalSet)
        .then((response) => {
          this.$message({
            type: "success",
            message: "新增成功!",
          });
          this.$router.push({ path: "/yygh/hospitalSet/list" });
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "新增失败!",
          });
        });
    },
    update() {
      hospitalSetApi.updateHospitalSet(this.hospitalSet)
      .then((response) => {
          this.$message({
            type: "success",
            message: "更新成功!",
          });
          this.$router.push({ path: "/yygh/hospitalSet/list" });
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "更新失败!",
          });
        });
    },

    //若路由中有参数id获取到并回显
    getHospitalSetById(id) {
      hospitalSetApi.getHospitalSetById(id).then((response) => {
        this.hospitalSet = response.data.hospitalSet;
      });
    },
  },
};
</script>
